package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.DeptDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.LoanDAO;
import com.trkj.dao.LoanlogDAO;
import com.trkj.pojo.Dept;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Loan;
import com.trkj.pojo.Loanlog;
import com.trkj.vo.LoanEmpVo;


@Service
@Transactional
public class LoanService {
	@Autowired
	WorkFlowService workFlowService;
	@Autowired
	EmpDAO empDAO;
	@Autowired
	DeptDAO deptDAO;
	@Autowired
	LoanDAO loanDAO;
	@Autowired
	BackLogProcessService backLogProcessService;
	@Autowired
	LoanlogDAO loanlogDAO;
	
	/**
	 * 带条件查询借款数据
	 *  @author csj
	 * @param empName
	 * @param deptName
	 * @return
	 */
	public List<Map<String, Object>> checkempLoanInfo(String empName,String deptName) {
		List<Object[]> list=null;
		List<Map<String, Object>> list2 = new ArrayList<>();
		if(empName!=null&&deptName!=null) {
			list = empDAO.findemploanANDtiaojian(empName+"%",deptName+"%");
		}else if (empName!=null||deptName!=null) {
			list = empDAO.findemploanORtiaojian(empName+"%",deptName+"%");
		}else {
			list = empDAO.findemploanInfo();
		}
		for (Object[] objects : list) {
			Map<String, Object> map = new HashMap<>();
			map.put("empId", objects[0]);
			map.put("empName", objects[1]);
			map.put("deptName", objects[2]);
			map.put("empLoanbalance", objects[3]);
			list2.add(map);
		}
		return list2;
	}
	/**
	 * 暂存一个借款申请
	 * @author Lixd
	 * @param loan
	 * @param empId
	 * @param deptId
	 * @return 
	 */
	public Integer save_loan(Loan loan,Integer empId,Integer deptId) {
		try {
			Emp emp = empDAO.findOne(empId);
			Dept dept = deptDAO.findOne(deptId);
			loan.setEmp(emp);
			loan.setDept(dept);
			loan.setLunchPeople(empId);
			return loanDAO.save(loan).getLoanId();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	/**
	 * 得到自动填入的emp的信息
	 * @author Lixd
	 * @param emp
	 * @return
	 */
	public Map<String, Object> getEmpInfo(Integer empId){
		Map<String, Object> map = new HashMap<>();
		Emp emp = empDAO.findOne(empId);
		map.put("empId", empId);
		map.put("loanName", emp.getEmpName());
		map.put("empLoanbalance", emp.getEmpLoanbalance());
		Dept dept = emp.getDept();
		if (dept!=null) {
			map.put("deptId", dept.getDeptId());
			map.put("loanDept", dept.getDeptName());
		}
		Date date = new Date();
		map.put("loanUptime", new SimpleDateFormat("yyyy-MM-dd").format(date));
		return map;
	}
	
	/**
	 * 查询编辑所需要显现的数据
	 *  @author csj
	 * @param empId
	 * @return
	 */
	public LoanEmpVo editLoan(Integer empId) {
		Emp emp = empDAO.findOne(empId);
		LoanEmpVo vo = new LoanEmpVo();
		vo.setEmpId(emp.getEmpId()+"");
		vo.setEmpName(emp.getEmpName());
		vo.setLoanlogLastmoney(emp.getEmpLoanbalance()+"");
		vo.setLoanlogThismoney(emp.getEmpLoanbalance()+"");
		return vo;
	}
	
	/**
	 * 将编辑的借款记录保存到loanlog中
	 * @author csj
	 * @param loanlog
	 * @param empId
	 * @return
	 */
	public Integer saveLoanInfo(Loanlog loanlog,Integer empId) {
		try {
			if (loanlog!=null) {
				Date date = new Date();
				loanlog.setLoanlogTime(date);
				if(loanlog.getLoanlogFiow()==null) {
					loanlog.setLoanlogFiow("无关联流程操作");
				}
				if (empId!=null) {
					Emp emp = empDAO.findOne(empId);
					BigDecimal thismoney = loanlog.getLoanlogThismoney();
					emp.setEmpLoanbalance(thismoney.intValue());
					loanlog.setEmp(emp);
				}
				loanlogDAO.save(loanlog);
				return 1;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 查询带条件的个人借款日志
	 *  @author csj
	 * @param empId
	 * @param onetime
	 * @param lasttime
	 * @return
	 */
	public List<LoanEmpVo> loanlogInfo(Integer empId,Date onetime,Date lasttime) {
		List<Object[]> list=null;
		if (onetime!=null&&lasttime!=null) {
			list = loanlogDAO.findLoanlogANDInfo(empId,onetime,lasttime);
		}else {
			list = loanlogDAO.findLoanlogInfo(empId);
		}
		List<LoanEmpVo> listvo = new ArrayList<>();
		for (Object[] objects : list) {
			LoanEmpVo vo = new LoanEmpVo();
			vo.setEmpId(objects[1]+"");
			vo.setLoanlogTime(objects[2]+"");
			vo.setLoanlogLastmoney(objects[3]+"");
			vo.setLoanlogThismoney(objects[4]+"");
			vo.setLoanlogFiow(objects[5]+"");
			vo.setLoanlogNote(objects[6]+"");
			vo.setEmpName(objects[7]+"");
			listvo.add(vo);
		}
		return listvo;
	}
	/**
	 * 保存借款日志信息（流程端）
	 * @author Lixd
	 * @param loanProcessId
	 * @param processName
	 * @return
	 */
	public boolean saveLoanInfo(Integer loanProcessId) {
		Loan loan = loanDAO.findOne(loanProcessId);
		if (loan!=null) {
			Emp emp = loan.getEmp();
			Loanlog loanlog = new Loanlog();
			loanlog.setEmp(emp);
			loanlog.setLoanlogFiow(loanProcessId+"");
			loanlog.setLoanlogLastmoney(new BigDecimal(emp.getEmpLoanbalance()));
			BigDecimal decimal = new BigDecimal(emp.getEmpLoanbalance()).add(loan.getLoanMoney());
			loanlog.setLoanlogThismoney(decimal);
			emp.setEmpLoanbalance(decimal.intValue());
			loanlog.setLoanlogTime(new Date());
			loanlog.setLoanlogNote(loan.getLoanExplain());
			loanlogDAO.save(loanlog);
			return true;
		}
		return false;
	}
}
